package com.sduept.nwld.dataserver.rest;

import java.util.List;
import java.util.TreeSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sduept.bigdata.weather.utils.TowerSerialNoComparator;
import com.sduept.cim.model.core.BaseVoltage;
import com.sduept.cim.model.extend.LineTower;
import com.sduept.cim.model.wires.ACLineSegment;
import com.sduept.core.dao.SuperDAO;

/**
 * @author laiy
 */
@RestController
@RequestMapping("/")
public class LineServices {

	@Autowired
	private SuperDAO dao;


	@GetMapping("/getLineTowerByName/{linename}")
	public String getLineTowerByName(@PathVariable("linename") String linename) {
		JSONArray rtn = new JSONArray();
		JSONArray ns = JSON.parseArray(linename);
		String[] nams = ns.toArray(new String[0]);
		for (String n : nams) {
//			List<LineTower> lts = dao.findByCondition("lineName='"+n+"'", LineTower.class);
			List<LineTower> lts = dao.findByCondition(LineTower.class, "lineName = ?", n);
			if (lts.size() > 0) {
				JSONArray line = new JSONArray();
				line.add(n);
				line.add(getTowerJsonArray(lts));
				rtn.add(line);
			}
		}
		return rtn.toJSONString();
	}


	@GetMapping("/getLineTowerByVoltage/{voltage}")
	public String getLineTowerByVoltage(@PathVariable("voltage") String voltage) {
		JSONArray rtn = new JSONArray();
//		List<BaseVoltage> vs = dao.findByCondition(" nominalVoltage >= "+voltage.toLowerCase().replaceAll("kv", "")+"", BaseVoltage.class);
		List<BaseVoltage> vs = dao.findByCondition(BaseVoltage.class, " nominalVoltage >= ?",
				voltage.toLowerCase().replaceAll("kv", ""));
		if (vs.size() > 0) {
			for (BaseVoltage baseVoltage : vs) {
//				List<ACLineSegment> acls = dao.findByCondition("baseVoltage='"+baseVoltage.getId()+"'", ACLineSegment.class);
				List<ACLineSegment> acls = dao.findByCondition(ACLineSegment.class, "baseVoltage = ?",
						baseVoltage.getId());
				for (ACLineSegment acLineSegment : acls) {
//					List<LineTower> lts = dao.findByCondition("lineName='" + acLineSegment.getName() + "'",
//							LineTower.class);
					List<LineTower> lts = dao.findByCondition(LineTower.class, "lineName = ?", acLineSegment.getName());
					if (lts.size() > 0) {
						JSONArray line = new JSONArray();
						line.add(acLineSegment.getName());
						line.add(getTowerJsonArray(lts));
						rtn.add(line);
					}
				}
			}
		}

		return rtn.toJSONString();
	}

	private JSONArray getTowerJsonArray(List<LineTower> lts) {
		TreeSet<LineTower> sorted = new TreeSet<>(new TowerSerialNoComparator());
		sorted.addAll(lts);
		JSONArray jarray = new JSONArray(lts.size() * 2);
		double[] pre = null;
		for (LineTower lineTower : sorted) {
			if (pre != null) {
				double[] current = new double[2];
				current[0] = lineTower.getLongitude();
				current[1] = lineTower.getLatitude();
				double[][] dss = new double[][] { current, pre };
				jarray.add(dss);
				pre = current;
			} else {
				pre = new double[2];
				pre[0] = lineTower.getLongitude();
				pre[1] = lineTower.getLatitude();
			}

		}
		return jarray;
	}
}
